var merchant = {
	monthly_deposit_init : function() {		
		$('table.monthly_deposit_tbl > tbody').find('tr').each(function(index, elmt){
			$(this).find('input#total_deposit').blur(function() {
				merchant.calculate_avr_monthly_deposit();
			});
		});
	},
	
	calculate_avr_monthly_deposit : function() {
		var total_deposit = 0;
		
		var count = 0;
		$('table.monthly_deposit_tbl > tbody').find('tr').each(function(index, elmt){
			var ele = $(this).find('input#total_deposit');
			
			if(ele.val().length > 0 && isNaN(ele.val()) === false) {				
				total_deposit += parseFloat($(this).find('input#total_deposit').val());
				count++;
			}
		});

		var avr_monthly_deposit = "0.00";
		if(count > 0) {
			avr_monthly_deposit = (parseFloat(total_deposit)/parseInt(count)).toFixed(2);
		}
		
		$('input#avr_monthly_deposit').val(avr_monthly_deposit);
	},
	
	duplicate_case : function(case_id) {
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		if(confirm('Proceed to create a new application using current information?')){
			var callback = function(json){
	            if(json.status == 'OK'){
	            	
	            	window.location.href = jq_main_url + '/NewApp.do?dispatch=init&m_id=' + encodeURIComponent(json.merchant_id) + '&c_id=' + encodeURIComponent(json.case_id);
	            	
	            } else {
	            	mapUI.jq_message(json.message, 10);
	            }
	        };
			
	        var jq = {
	            'method' : 'POST',
	            'url' : jq_main_url + '/SubmitApp.do?dispatch=duplicate_case',
	            'postdata' : postdata
	        };
			
	        mapUI.jq_call(jq, callback);
		}
	},
	
	create_business_detail : function() {
		var elmt, postdata;
		
		elmt = document.getElementById('app_type');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Please choose Type of Application.', 10);
			return;
		}
		postdata = 'app_type=' + encodeURIComponent(elmt.value);
		
		var ownership_status = "";
		$('#ownership_status').find('input[type=radio]').each(function(index, elmt){
            if($(this).is(':checked')){
            	ownership_status = $(this).val();
            }
        });
		
		if(ownership_status.length <= 0){
			mapUI.jq_message('Please choose an Ownership Status.', 10);
			return;
		}
		postdata += '&ownership_status=' + encodeURIComponent(ownership_status);
		
		elmt = document.getElementById('registered_name');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Registered Name must not be blank.', 10);
			return;
		}
		if(elmt.value.length > 30){
			mapUI.jq_message('Registered Name must not be longer than 30 characters.', 10);
			return;
		}
		postdata += '&registered_name=' + encodeURIComponent(elmt.value);
		
		
		elmt = document.getElementById('registered_no');
		if(elmt.value.length > 0){
			var regexp = /^[A-Za-z0-9]+$/
			if(regexp.test(elmt.value) == false){
				mapUI.jq_message('Registered No. must have only alphabet or number.', 10);
				return;
			}
		}
		postdata += '&registered_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('nature_of_business');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Nature of Business must not be blank.', 10);
			return;
		}
		postdata += '&nature_of_business=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('mcc_code');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('MCC Code must not be blank.', 10);
			return;
		}
		
		regexp = /^[0-9]{1,4}$/
		if(regexp.test(elmt.value) == false){
			mapUI.jq_message('MCC Code must be a max 4 digits numeric value.', 10);
			return;
		}
		
		postdata += '&mcc_code=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('paid_up_capital');
		if(elmt.value.length > 0){
			if(elmt.value.is_number() == false){
				mapUI.jq_message('Paid up Capital must not be a number.', 10);
				return;
			}
		}
		
		postdata += '&paid_up_capital=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('recall_barcode_no');
		postdata += '&recall_barcode_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('gst_registration');
		if(elmt.value.length > 0){
			if(elmt.value.is_number() == false){
				mapUI.jq_message('GST registration  must be a number.', 10);
				return;
			}
		}
		postdata += '&gst_registration=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('rmcd_approval_no'); 
		postdata += '&rmcd_approval_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('years_in_business');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('No. of Years in Business must not be blank.', 10);
			return;
		}
				
		postdata += '&years_in_business=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('avr_ticket_size');
		if(elmt.value.length > 0){
			regexp = /^[0-9]+$/
			if(regexp.test(elmt.value) == false){
				mapUI.jq_message('Average Ticket Size must be a number.', 10);
				return;
			} else {
				if(elmt.value > 10000){
					mapUI.jq_message('Average Ticket Size must be less than 10000.', 10);
					return;
				}
			}
		}
		postdata += '&avr_ticket_size=' + encodeURIComponent(elmt.value);
		
		
		elmt = document.getElementById('current_acquirer');
		postdata += '&current_acquirer=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('bank_name');
		postdata += '&bank_name=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('current_acc_no');
		postdata += '&current_acc_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('avr_monthly_deposit');
		if(elmt.value.length > 0){
			if(elmt.value.is_number() == false){
				mapUI.jq_message('Average Monthly Deposit must not be a number.', 10);
				return;
			}
		}
		
		postdata += '&avr_monthly_deposit=' + encodeURIComponent(elmt.value);
		
		
		var monthly_deposit = [];
		$('table.monthly_deposit_tbl').children('tbody').children('tr').each(function(index, elmt){
			var sel = $(this).children('td:eq(0)').children('select');
			var in1 = $(this).children('td:eq(1)').children('input');
			var in2 = $(this).children('td:eq(2)').children('input');
			
			monthly_deposit.push({
				'month' : sel.val(),
				'total_deposit' : in1.val(),
				'balance' : in2.val()
			});
		});
		
		postdata += '&monthly_deposit=' + encodeURIComponent(JSON.stringify(monthly_deposit));
		
		
		var amex_auth_sign = [];
		
		if($('#name_1').val().length > 38 || $('#name_2').val().length > 38){
			mapUI.jq_message('Amex Authorized Signatories Name must not be longer than 38 characters.', 10);
			return;
		}
		
		if($('#designation_1').val().length > 32 || $('#designation_2').val().length > 32){
			mapUI.jq_message('Amex Authorized Signatories Designation must not be longer than 32 characters.', 10);
			return;
		}
		
		if($('#ic_1').val().length > 16 || $('#ic_2').val().length > 16){
			mapUI.jq_message('Amex Authorized Signatories New IC / Passport No. must not be longer than 16 characters.', 10);
			return;
		}
		
		if($('#ic_1').val().length > 0){
			var regexp = /^[A-Za-z0-9]+$/
			if(regexp.test($('#ic_1').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories New IC / Passport No. must have only alphabet or number.', 10);
				return;
			}
		}
		
		if($('#ic_2').val().length > 0){
			var regexp = /^[A-Za-z0-9]+$/
			if(regexp.test($('#ic_2').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories New IC / Passport No. must have only alphabet or number.', 10);
				return;
			}
		}
		
		if($('#dob_1').val().length > 0){
			var regexp = /^[0-9]{2}[0-9]{2}[0-9]{4}$/
			if(regexp.test($('#dob_1').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories Date of Birth must be in ddmmyyyy format.', 10);
				return;
			}
		}
		
		if($('#dob_2').val().length > 0){
			var regexp = /^[0-9]{2}[0-9]{2}[0-9]{4}$/
			if(regexp.test($('#dob_2').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories Date of Birth must be in ddmmyyyy format.', 10);
				return;
			}
		}
		
		amex_auth_sign.push({
			'title' : $('#name_1').val().length > 0 ? $('#title_1').val() : '',
			'name' : $('#name_1').val(),
			'designation' : $('#name_1').val().length > 0 ? ($('#designation_1').val().length > 0 ? $('#designation_1').val() : '0') : '0',
			'ic_no' : $('#name_1').val().length > 0 ? ($('#ic_1').val().length <= 0 ? '0' : $('#ic_1').val()) : '',
			'date_of_birth' : $('#name_1').val().length > 0 ? ($('#dob_1').val().length <= 0 ? '00000000' : $('#dob_1').val()) : '',
			'gender' : $('#name_1').val().length > 0 ? $('#gender_1').val() : '0',
		});
		
		amex_auth_sign.push({
			'title' : $('#name_2').val().length > 0 ? $('#title_2').val() : '',
			'name' : $('#name_2').val(),
			'designation' : $('#name_2').val().length > 0 ? ($('#designation_2').val().length > 0 ? $('#designation_2').val() : '0') : '0',
			'ic_no' : $('#name_2').val().length > 0 ? ($('#ic_2').val().length <= 0 ? '00' : $('#ic_2').val()) : '',
			'date_of_birth' : $('#name_2').val().length > 0 ? ($('#dob_2').val().length <= 0 ? '00000000' : $('#dob_2').val()) : '',
			'gender' : $('#name_2').val().length > 0 ? $('#gender_2').val() : '0',
		});
		
		postdata += '&amex_auth_sign=' + encodeURIComponent(JSON.stringify(amex_auth_sign));
		
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Business Detail has been added successfully.", 10);
            	   
            	
            	var click_redirect = $('input#click_redirect').val();
            	if(click_redirect.length > 0) {
					var sep = click_redirect.indexOf("?") >= 0 ? "&" : "?";
            		window.location.href = jq_main_url + click_redirect + sep + "m_id=" + json.merchant_id + "&c_id=" + json.case_id;
            	}
            	else {
            		window.location.href = jq_main_url + "/NewApp.do?dispatch=init&m_id=" + json.merchant_id + "&c_id=" + json.case_id;
            	}
            	
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=create_business_detail',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback);
	},
	
	edit_business_detail : function(merchant_id, case_id) {
		var elmt, postdata;
		
		postdata = 'merchant_id=' + encodeURIComponent(merchant_id);

		postdata += '&case_id=' + encodeURIComponent(case_id);
		
		elmt = document.getElementById('app_type');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Please choose Type of Application.', 10);
			return;
		}
		postdata += '&app_type=' + encodeURIComponent(elmt.value);
		
		var ownership_status = "";
		$('#ownership_status').find('input[type=radio]').each(function(index, elmt){
            if($(this).is(':checked')){
            	ownership_status = $(this).val();
            }
        });
		
		if(ownership_status.length <= 0){
			mapUI.jq_message('Please choose an Ownership Status.', 10);
			return;
		}
		postdata += '&ownership_status=' + encodeURIComponent(ownership_status);
		
		elmt = document.getElementById('registered_name');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Registered Name must not be blank.', 10);
			return;
		}
		if(elmt.value.length > 30){
			mapUI.jq_message('Registered Name must not be longer than 30 characters.', 10);
			return;
		}
		postdata += '&registered_name=' + encodeURIComponent(elmt.value);
		
		
		elmt = document.getElementById('registered_no');
		if(elmt.value.length > 0){
			var regexp = /^[A-Za-z0-9]+$/
			if(regexp.test(elmt.value) == false){
				mapUI.jq_message('Registered No. must have only alphabet or number.', 10);
				return;
			}
		}
		postdata += '&registered_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('nature_of_business');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Nature of Business must not be blank.', 10);
			return;
		}
		postdata += '&nature_of_business=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('mcc_code');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('MCC Code must not be blank.', 10);
			return;
		}
		
		regexp = /^[0-9]{1,4}$/
		if(regexp.test(elmt.value) == false){
			mapUI.jq_message('MCC Code must be a max 4 digits numeric value.', 10);
			return;
		}
		
		postdata += '&mcc_code=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('paid_up_capital');
		if(elmt.value.length > 0){
			if(elmt.value.is_number() == false){
				mapUI.jq_message('Paid up Capital must not be a number.', 10);
				return;
			}
		}
		
		postdata += '&paid_up_capital=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('recall_barcode_no');
		postdata += '&recall_barcode_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('gst_registration');
		postdata += '&gst_registration=' + encodeURIComponent(elmt.value);
		
		if(elmt.value.length > 0){
			if(elmt.value.is_number() == false){
				mapUI.jq_message('GST Registration must not be a number.', 10);
				return;
			}
		} 
		
		elmt = document.getElementById('rmcd_approval_no');
		postdata += '&rmcd_approval_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('years_in_business');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('No. of Years in Business must not be blank.', 10);
			return;
		}
		postdata += '&years_in_business=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('avr_ticket_size');
		if(elmt.value.length > 0){
			regexp = /^[0-9]+$/
			if(regexp.test(elmt.value) == false){
				mapUI.jq_message('Average Ticket Size must be a number.', 10);
				return;
			} else {
				if(elmt.value > 10000){
					mapUI.jq_message('Average Ticket Size must be less than 10000.', 10);
					return;
				}
			}
		}
		postdata += '&avr_ticket_size=' + encodeURIComponent(elmt.value);
		
		
		elmt = document.getElementById('current_acquirer');
		postdata += '&current_acquirer=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('bank_name');
		postdata += '&bank_name=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('current_acc_no');
		postdata += '&current_acc_no=' + encodeURIComponent(elmt.value);
		
		elmt = document.getElementById('avr_monthly_deposit');
		if(elmt.value.length > 0){
			if(elmt.value.is_number() == false){
				mapUI.jq_message('Average Monthly Deposit must not be a number.', 10);
				return;
			}
		}
		
		postdata += '&avr_monthly_deposit=' + encodeURIComponent(elmt.value);
		
		
		var monthly_deposit = [];
		$('table.monthly_deposit_tbl').children('tbody').children('tr').each(function(index, elmt){
			var sel = $(this).children('td:eq(0)').children('select');
			var in1 = $(this).children('td:eq(1)').children('input');
			var in2 = $(this).children('td:eq(2)').children('input');
			
			monthly_deposit.push({
				'month' : sel.val(),
				'total_deposit' : in1.val(),
				'balance' : in2.val()
			});
		});
		
		postdata += '&monthly_deposit=' + encodeURIComponent(JSON.stringify(monthly_deposit));
		
		
		var amex_auth_sign = [];
		
		if($('#name_1').val().length > 38 || $('#name_2').val().length > 38){
			mapUI.jq_message('Amex Authorized Signatories Name must not be longer than 38 characters.', 10);
			return;
		}
		
		if($('#designation_1').val().length > 32 || $('#designation_2').val().length > 32){
			mapUI.jq_message('Amex Authorized Signatories Designation must not be longer than 32 characters.', 10);
			return;
		}
		
		if($('#ic_1').val().length > 16 || $('#ic_2').val().length > 16){
			mapUI.jq_message('Amex Authorized Signatories New IC / Passport No. must not be longer than 16 characters.', 10);
			return;
		}
		
		if($('#ic_1').val().length > 0){
			var regexp = /^[A-Za-z0-9]+$/
			if(regexp.test($('#ic_1').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories New IC / Passport No. must have only alphabet or number.', 10);
				return;
			}
		}
		
		if($('#ic_2').val().length > 0){
			var regexp = /^[A-Za-z0-9]+$/
			if(regexp.test($('#ic_2').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories New IC / Passport No. must have only alphabet or number.', 10);
				return;
			}
		}
		
		if($('#dob_1').val().length > 0){
			var regexp = /^[0-9]{2}[0-9]{2}[0-9]{4}$/
			if(regexp.test($('#dob_1').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories Date of Birth must be in ddmmyyyy format.', 10);
				return;
			}
		}
		
		if($('#dob_2').val().length > 0){
			var regexp = /^[0-9]{2}[0-9]{2}[0-9]{4}$/
			if(regexp.test($('#dob_2').val()) == false){
				mapUI.jq_message('Amex Authorized Signatories Date of Birth must be in ddmmyyyy format.', 10);
				return;
			}
		}
		
		amex_auth_sign.push({
			'title' : $('#name_1').val().length > 0 ? $('#title_1').val() : '',
			'name' : $('#name_1').val(),
			'designation' : $('#name_1').val().length > 0 ? ($('#designation_1').val().length > 0 ? $('#designation_1').val() : '0') : '0',
			'ic_no' : $('#name_1').val().length > 0 ? ($('#ic_1').val().length <= 0 ? '0' : $('#ic_1').val()) : '',
			'date_of_birth' : $('#name_1').val().length > 0 ? ($('#dob_1').val().length <= 0 ? '00000000' : $('#dob_1').val()) : '',
			'gender' : $('#name_1').val().length > 0 ? $('#gender_1').val() : '0',
		});
		
		amex_auth_sign.push({
			'title' : $('#name_2').val().length > 0 ? $('#title_2').val() : '',
			'name' : $('#name_2').val(),
			'designation' : $('#name_2').val().length > 0 ? ($('#designation_2').val().length > 0 ? $('#designation_2').val() : '0') : '0',
			'ic_no' : $('#name_2').val().length > 0 ? ($('#ic_2').val().length <= 0 ? '00' : $('#ic_2').val()) : '',
			'date_of_birth' : $('#name_2').val().length > 0 ? ($('#dob_2').val().length <= 0 ? '00000000' : $('#dob_2').val()) : '',
			'gender' : $('#name_2').val().length > 0 ? $('#gender_2').val() : '0',
		});
		
		postdata += '&amex_auth_sign=' + encodeURIComponent(JSON.stringify(amex_auth_sign));
		
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Business Detail has been modified successfully.", 10);
            	
            	var click_redirect = $('input#click_redirect').val();
            	if(click_redirect.length > 0) {
					var sep = click_redirect.indexOf("?") >= 0 ? "&" : "?";
					var q = window.location.search.replace(/^\?dispatch=[\w]+&/, "");
            		window.location.href = jq_main_url + click_redirect + sep + q;
            	}
            	
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=edit_business_detail',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback);
	},
	
	send_match_trace : function(merchant_id, case_id) {
		merchant.create_match_trace(merchant_id, case_id, 1);
	},
	
	create_match_trace : function(merchant_id, case_id, send_to_host) {
		var elmt, postdata;
		
		postdata = 'merchant_id=' + encodeURIComponent(merchant_id);
		postdata += '&case_id=' + encodeURIComponent(case_id);
		
		if(send_to_host == null){
			postdata += '&send_to_host=0';
		} else {
			postdata += '&send_to_host=' + encodeURIComponent(send_to_host);
		}
		
		
		elmt = document.getElementById('app_type');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Please choose Type of Application.', 10);
			return;
		}
		postdata += '&app_type=' + encodeURIComponent(elmt.value);
		
		var ownership_status = "";
		$('#ownership_status').find('input[type=radio]').each(function(index, elmt){
            if($(this).is(':checked')){
            	ownership_status = $(this).val();
            }
        });
		
		if(ownership_status.length <= 0){
			mapUI.jq_message('Please choose an Ownership Status.', 10);
			return;
		}
		postdata += '&ownership_status=' + encodeURIComponent(ownership_status);
		
		
		var match_trace = [];
		var mt_err = false;
		
		$('table.match_trace_tbl').children('tbody').children('tr').each(function(index, elmt){
			var in1 = $(this).children('td:eq(0)').children('input');
			var in2 = $(this).children('td:eq(1)').children('input');
			var in3 = $(this).children('td:eq(2)').children('input');
			var sel = $(this).children('td:eq(3)').children('select');
			var in4 = $(this).children('td:eq(4)').children('input');
			
			if(mt_err == true){
				return;
			}
			
			if(in1.val().length > 15){
				mapUI.jq_message('First Name must not be longer than 15 characters.', 10);
				mt_err = true;
				return;
			}
			
			if(in2.val().length > 25){
				mapUI.jq_message('Last Name must not be longer than 25 characters.', 10);
				mt_err = true;
				return;
			}
			
			if(in3.val().length > 25){
				mapUI.jq_message('Owner IC/Passport must not be longer than 25 characters.', 10);
				mt_err = true;
				return;
			}
			
			match_trace.push({
				'f_name' : in1.val(),
				'l_name' : in2.val(),
				'ic' : in3.val(),
				'nationality' : sel.val(),
				'country_code' : in4.val()
			});
		});
		
		
		if(mt_err == true){
			return;
		}
		
		postdata += '&match_trace=' + encodeURIComponent(JSON.stringify(match_trace));
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Match / Trace has been saved successfully.", 10);
            	
            	var click_redirect = $('input#click_redirect').val();
            	if(click_redirect.length > 0) {
            		var sep = click_redirect.indexOf("?") >= 0 ? "&" : "?";
					var q = window.location.search.replace(/^\?dispatch=[\w]+&/, "");
            		window.location.href = jq_main_url + click_redirect + sep + q;
            	}
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=create_match_trace',
            'postdata' : postdata
        };
	
        
        mapUI.jq_call(jq, callback);
	},
	
	add_match_trace_entry : function() {
		var sel = $('table.match_trace_tbl > tbody').children('tr:eq(0)').children('td:eq(3)').children('select');
		var tr = '<tr data-index="' + ++index + '">'
			+ '<td><input type="text" name="f_name" /></td>'
			+ '<td><input type="text" name="l_name" /></td>'
			+ '<td><input type="text" name="owner_ic" /></td>'
			+ '<td><select name="nationality">' + nationality_opt + '</select></td>'
			+ '<td><input type="text" name="country_code" readonly="readonly" value="MYS"/></td>'
			+ '<td><a href="javascript:delete_item(' + index + ')">Delete</a></td>'
		+ '</tr>';
		
		//tr = $(tr);
		//tr.children('td:eq(3)').append(sel.clone());
		
		$('table.match_trace_tbl > tbody').append(tr);
	},
	
	delete_match_trace_entry : function(index) {
		if(confirm('Are you sure to delete?')){
			$('table.match_trace_tbl > tbody').find('tr[data-index=' + index + ']:eq(0)').remove();
		}
	},
	
	document_checklist_load : function(){
		$(document.form.business_type).on('change', function(event){
			$('div.document').children('.tbl, .control-group').removeClass('active');
			$('div.document').find('input[type=checkbox]').attr('checked', false);
			$('div.document').find('textarea').val('');
			
			var business_type = $(this).val();
			
			switch(business_type){
			case 'For Private Limited Company':
				$('div.document').children('.private_limited').addClass('active');
				break;
				
			case 'For Sole Proprietor & Partnership':
				$('div.document').children('.sole_proprietor_partnership').addClass('active');
				break;
				
			case 'For Clinic':
				$('div.document').children('.clinic').addClass('active');
				break;
				
			case 'For JMB':
				$('div.document').children('.jmb').addClass('active');
				break;
				
			case 'For Law Firm':
				$('div.document').children('.law_firm').addClass('active');
				break;
				
			case 'For Association, Charity Body, etc':
				$('div.document').children('.association').addClass('active');
				break;
				
			default:
				break;
			}
		});
	},
	
	create_document_checklist : function(merchant_id, case_id, notify_email) {
		var elmt, postdata;
		
		postdata = 'merchant_id=' + encodeURIComponent(merchant_id);
		postdata += '&case_id=' + encodeURIComponent(case_id);
		
		if(notify_email == true){
			postdata += '&notify_email=Y';
		} else {
			postdata += '&notify_email=N';
		}
		
		var data = {};
		var general = {};
		
		$('table.general').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required = $(this).children('td.not_required').children('input[type=checkbox]');
			var cb_available = $(this).children('td.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			general[name] = {
				'not_required' : cb_not_required.is(':checked') ? cb_not_required.val() : '',
				'available' : cb_available.is(':checked') ? cb_available.val() : ''
			};
		});
		
		
		data['general'] = general;
		
		
		var elmt = document.form.business_type;
		data['business_type'] = elmt.value;
		
		var private_limited = {};
		
		$('table.private_limited').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required_1 = $(this).children('td.a.not_required').children('input[type=checkbox]');
			var cb_available_1 = $(this).children('td.a.available').children('input[type=checkbox]');
			var cb_not_required_2 = $(this).children('td.b.not_required').children('input[type=checkbox]');
			var cb_available_2 = $(this).children('td.b.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			private_limited[name] = {
				'not_required_1' : cb_not_required_1.is(':checked') ? cb_not_required_1.val() : '',
				'available_1' : cb_available_1.is(':checked') ? cb_available_1.val() : '',
				'not_required_2' : cb_not_required_2.is(':checked') ? cb_not_required_2.val() : '',
				'available_2' : cb_available_2.is(':checked') ? cb_available_2.val() : ''
			};
		});
		
		
		private_limited['remarks'] = $('div.private_limited').children('textarea').val();
		
		data['private_limited'] = private_limited;
		
		
		var sole_proprietor_partnership = {};
		
		$('table.sole_proprietor_partnership').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required_1 = $(this).children('td.a.not_required').children('input[type=checkbox]');
			var cb_available_1 = $(this).children('td.a.available').children('input[type=checkbox]');
			var cb_not_required_2 = $(this).children('td.b.not_required').children('input[type=checkbox]');
			var cb_available_2 = $(this).children('td.b.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			sole_proprietor_partnership[name] = {
				'not_required_1' : cb_not_required_1.is(':checked') ? cb_not_required_1.val() : '',
				'available_1' : cb_available_1.is(':checked') ? cb_available_1.val() : '',
				'not_required_2' : cb_not_required_2.is(':checked') ? cb_not_required_2.val() : '',
				'available_2' : cb_available_2.is(':checked') ? cb_available_2.val() : ''
			};
		});
		
		
		sole_proprietor_partnership['remarks'] = $('div.sole_proprietor_partnership').children('textarea').val();
		
		data['sole_proprietor_partnership'] = sole_proprietor_partnership;
		
		
		var clinic = {};
		
		$('table.clinic').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required_1 = $(this).children('td.a.not_required').children('input[type=checkbox]');
			var cb_available_1 = $(this).children('td.a.available').children('input[type=checkbox]');
			var cb_not_required_2 = $(this).children('td.b.not_required').children('input[type=checkbox]');
			var cb_available_2 = $(this).children('td.b.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			clinic[name] = {
				'not_required_1' : cb_not_required_1.is(':checked') ? cb_not_required_1.val() : '',
				'available_1' : cb_available_1.is(':checked') ? cb_available_1.val() : '',
				'not_required_2' : cb_not_required_2.is(':checked') ? cb_not_required_2.val() : '',
				'available_2' : cb_available_2.is(':checked') ? cb_available_2.val() : ''
			};
		});
		
		
		clinic['remarks'] = $('div.clinic').children('textarea').val();
		
		data['clinic'] = clinic;
		
		
		var jmb = {};
		
		$('table.jmb').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required_1 = $(this).children('td.a.not_required').children('input[type=checkbox]');
			var cb_available_1 = $(this).children('td.a.available').children('input[type=checkbox]');
			var cb_not_required_2 = $(this).children('td.b.not_required').children('input[type=checkbox]');
			var cb_available_2 = $(this).children('td.b.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			jmb[name] = {
				'not_required_1' : cb_not_required_1.is(':checked') ? cb_not_required_1.val() : '',
				'available_1' : cb_available_1.is(':checked') ? cb_available_1.val() : '',
				'not_required_2' : cb_not_required_2.is(':checked') ? cb_not_required_2.val() : '',
				'available_2' : cb_available_2.is(':checked') ? cb_available_2.val() : ''
			};
		});
		
		
		jmb['remarks'] = $('div.jmb').children('textarea').val();
		
		data['jmb'] = jmb;
		
		
		var law_firm = {};
		
		$('table.law_firm').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required_1 = $(this).children('td.a.not_required').children('input[type=checkbox]');
			var cb_available_1 = $(this).children('td.a.available').children('input[type=checkbox]');
			var cb_not_required_2 = $(this).children('td.b.not_required').children('input[type=checkbox]');
			var cb_available_2 = $(this).children('td.b.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			law_firm[name] = {
				'not_required_1' : cb_not_required_1.is(':checked') ? cb_not_required_1.val() : '',
				'available_1' : cb_available_1.is(':checked') ? cb_available_1.val() : '',
				'not_required_2' : cb_not_required_2.is(':checked') ? cb_not_required_2.val() : '',
				'available_2' : cb_available_2.is(':checked') ? cb_available_2.val() : ''
			};
		});
		
		
		law_firm['remarks'] = $('div.law_firm').children('textarea').val();
		
		data['law_firm'] = law_firm;
		
		
		var association = {};
		
		$('table.association').children('tbody').children('tr').each(function(index, elmt){
			var cb_not_required_1 = $(this).children('td.a.not_required').children('input[type=checkbox]');
			var cb_available_1 = $(this).children('td.a.available').children('input[type=checkbox]');
			var name = $(this).attr('data-name');
			
			association[name] = {
				'not_required_1' : cb_not_required_1.is(':checked') ? cb_not_required_1.val() : '',
				'available_1' : cb_available_1.is(':checked') ? cb_available_1.val() : ''
			};
		});
		
		association['remarks'] = $('div.association').children('textarea').val();
		
		data['association'] = association;
		
		
		
		postdata += '&document_checklist=' + encodeURIComponent(JSON.stringify(data));
		
		
		var callback = function(json){
            if(json.status == 'OK'){
            	if(notify_email == true){
            		mapUI.jq_message("Document Checklist notification email has been sent.", 10);
            	} else {
            		mapUI.jq_message("Document Checklist has been saved successfully.", 10);
            	}
            	
        		var click_redirect = $('input#click_redirect').val();
            	if(click_redirect.length > 0) {
					var sep = click_redirect.indexOf("?") >= 0 ? "&" : "?";
					var q = window.location.search.replace(/^\?dispatch=[\w]+&/, "");
					
            		window.location.href = jq_main_url + click_redirect + sep + q;
            	}
            	
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=create_document_checklist',
            'postdata' : postdata
        };

        mapUI.jq_call(jq, callback);
	},
	
	document_delete : function(case_id, merchant_id, doc_id){
		var elmt, postdata;
		
		postdata = 'doc_id=' + encodeURIComponent(doc_id);
		
		if(confirm('Proceed to delete this document?')){
			var callback = function(json){
	            if(json.status == 'OK'){
	            	mapUI.jq_message("Document removed successfully.", 10);
	            	window.location.href = jq_main_url + '/NewApp.do?dispatch=supporting_document&m_id=' + encodeURIComponent(merchant_id) + '&c_id=' + encodeURIComponent(case_id);
					
	            } else {
	            	mapUI.jq_message(json.message, 10);
	            }
	        };
			
	        var jq = {
	            'method' : 'POST',
	            'url' : jq_main_url + '/SubmitApp.do?dispatch=document_delete',
	            'postdata' : postdata
	        };
			
	        mapUI.jq_call(jq, callback);
		}
	},
	
	print_case : function(merchant_id, case_id) {
		var pWindow = window.open(jq_main_url + '/NewApp.do?dispatch=print_case&m_id=' + encodeURIComponent(merchant_id) + '&c_id=' + encodeURIComponent(case_id), "Case 20140000MY0082", "width=1200, height=600");
	},
	
	best_practice : function(case_id){
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		postdata += '&best_practice=' + encodeURIComponent(document.form1.best_practice.value);
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Setting successfully saved.", 10);
            	window.location.href = jq_main_url + '/NewApp.do?dispatch=supporting_document&m_id=' + encodeURIComponent(merchant_id) + '&c_id=' + encodeURIComponent(case_id);
				
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=best_practice',
            'postdata' : postdata
        };
		
        mapUI.jq_call(jq, callback);
	}
};